#include "baiduyun_manager.h"
#include "base/curl_client.h"
#include "base/url_encode.h"
#include "base/hash.h"


namespace imserver {

const std::string BaiduyunManager::apikey = "pW2rLFteCFthq2BQEfRYOQqd";
const std::string BaiduyunManager::secret_key = "cLdDRAHosYNRf8GKSDNBCqekLweHVTgX";

void BaiduyunManager::pushMsg(const std::string& baiduyun_user_id, const std::string& channel_id, const std::string& content, int64_t timestamp) {
  if (baiduyun_user_id.empty() || channel_id.empty()) {
    LOG_ERROR("BaiduyunManager::pushMsg --> baiduyun_user_id or channel_id is empty. content:" << content);
    return;
  }
  std::ostringstream os;
  os << "{\'title\':\'" << "微招聘" << "\'," <<
      "\'description\':\'" << content << "\'," <<
      "\'notification_builder_id\':0," <<
      "\'notification_basic_style\':7," <<
      "\'open_type\':2}";
  std::string msg_param = os.str();
  os.str("");
  os << "POSThttp://channel.api.duapp.com/rest/2.0/channel/channelapikey=" << apikey <<
      "channel_id=" << channel_id << "message_type=1messages=" << msg_param << "method=push_msgmsg_keys=msgkey" << 
      "push_type=1timestamp=" << timestamp << "user_id="<< baiduyun_user_id << secret_key;
  std::string sign_input = base::UrlEncode(os.str());
  std::string sign = base::HashMD5(sign_input.c_str(), sign_input.size());
  os.str("");
  os << "apikey=" << apikey << "&channel_id=" << channel_id << "&message_type=1&messages=" << base::UrlEncode(msg_param) <<
      "&method=push_msg&msg_keys=msgkey&push_type=1&sign=" << sign <<
      "&timestamp=" << timestamp << "&user_id=" << baiduyun_user_id;
  std::string params = os.str();
  std::string baiduyun_reponse;
  base::CurlClient::Instance().Post("http://channel.api.duapp.com/rest/2.0/channel/channel", params,
      &baiduyun_reponse);
  LOG_INFO("BaiduyunManager::pushMsg --> content:" << content << " timestamp:" << timestamp <<
      " params:" << params << " response:" << baiduyun_reponse);
}

}
